//
//  UIScrollView+Extension.swift
//  DoctorEPlus
//
//  Created by 王林波 on 2020/10/27.
//

import Foundation
import UIKit

extension UIScrollView {
    func rebuild() {
        if #available(iOS 11.0, *) {
            self.contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollIndicatorInsets = false
        }
    }
    
    
    func addRefresh(refresh: @escaping () -> Void) {
        let header = MJRefreshNormalHeader.init {
            refresh()
        }
        self.mj_header = header
    }
    
    func addloadMore(loadMore: @escaping () -> Void) {
        let footer = MJRefreshAutoNormalFooter.init {
            loadMore()
        }
        self.mj_footer = footer
    }
    
    func endRefresh()  {
        if self.mj_header?.isRefreshing == true {
            self.mj_header?.endRefreshing()
        }
    }
    
    func endLoading()  {
        if self.mj_footer?.isRefreshing == true {
            self.mj_footer?.endRefreshing()
        }
    }
    
    func endLoadWithNoMoreData(_ noMore: Bool) {
        if noMore == true {
            self.mj_footer?.endRefreshingWithNoMoreData()
        }else {
            self.mj_footer?.resetNoMoreData()
        }
    }

}

extension UIScrollView: UIGestureRecognizerDelegate {
    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if (gestureRecognizer.state != UIGestureRecognizer.State.possible) {
            return true
        }
        return false
    }
}

extension UITableView {
    static func buildTableView(style: UITableView.Style) -> UITableView {
        let tableView = UITableView.init(frame: CGRect.zero, style: style)
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.estimatedSectionHeaderHeight = 0
        tableView.backgroundColor = .white
        tableView.rebuild()
        return tableView
    }
    
    func regiseterNib(nibName: String)  {
        self.register(UINib.init(nibName: nibName, bundle: nil), forCellReuseIdentifier: nibName)
    }
    
    func regiseterClass<T>(cell: T) {
        self.register(T.self as! UITableViewCell.Type, forCellReuseIdentifier: NSStringFromClass(T.self as! AnyClass))
    }
}

extension UICollectionView {
    static func buildCollectionView(layout: UICollectionViewFlowLayout) -> UICollectionView {
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.backgroundColor = .white
        collectionView.rebuild()
        return collectionView
    }
    
    func regiseterNib(nibName: String)  {
        self.register(UINib.init(nibName: nibName, bundle: nil), forCellWithReuseIdentifier: nibName)
    }
    
   
}

